using System;

using System.Linq;
using System.Text;
using System.Transactions;
using System.Collections.Generic;
using Entity;
using Common.Background.Entity;

namespace Common.Background.Data
{

    /// <summary>
    /// 
    /// </summary>
    public class WebManageRolesRepository : BackgroundRepositoryBase, IWebManageRolesRepository
    {
        #region IWebManageRolesRepository 成员

        /// <summary>
        /// 职位设置
        /// </summary>
        /// <param name="WebManageRoles"></param>
        public void SetWebManageRoles(WebManageRoles webManageRoles)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    IWebManageRolesRepository iWebManageRolesRepository = new WebManageRolesRepository();
                    IWebManageRole_WebManageMenusRepository iWebManageRole_WebManageMenusRepository = new WebManageRole_WebManageMenusRepository();
                    iWebManageRole_WebManageMenusRepository.DeleteByRoleID(webManageRoles.ManageRoleID);
                    List<WebManageRole_WebManageMenus> list = new List<WebManageRole_WebManageMenus>();
                    foreach (var i in webManageRoles.WebManageRole_WebManageMenus_Extend)
                    {
                        list.Add(new WebManageRole_WebManageMenus { ManageRoleID = webManageRoles.ManageRoleID, MenuID = i.MenuID });
                    }
                    new WebManageRole_WebManageMenusRepository().Insert(list);
                    webManageRoles.UpdateDate = DateTime.Now.ToString("yy-MM-dd");
                    base.UpdateEntity(webManageRoles);
                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }
        }


        #endregion

        #region IRepository<WebManageRoles> 成员

        public void Update(WebManageRoles entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<WebManageRoles> list)
        {
            base.UpdateEntity(list.Cast<EntityBase>().ToList());
        }

        public void Insert(WebManageRoles entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<WebManageRoles> list)
        {
            base.InsertEntity(list.Cast<EntityBase>().ToList());
        }

        public WebManageRoles InsertGetIDENTITY(WebManageRoles entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.ManageRoleID);
        }

        public void Delete(WebManageRoles entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<WebManageRoles> list)
        {
            base.DeleteEntity(list.Cast<EntityBase>().ToList());
        }

        public IQueryable<WebManageRoles> GetModel()
        {
            return base.GetEntities<WebManageRoles>().Where(i => i.Status != (int)global::Entity.CommonEnums.Status.Deleted).Select(i => new WebManageRoles_Ext
            {
                About = i.About,
                DepartmentID = i.DepartmentID,
                ManageRoleID = i.ManageRoleID,
                Operator = i.Operator,
                OperatorAuthority = i.OperatorAuthority,
                RoleName = i.RoleName,
                SortNumber = i.SortNumber,
                Status = i.Status,
                UpdateDate = i.UpdateDate,
            });
        }

        public WebManageRoles Find(params object[] keyValues)
        {
            return base.GetEntity<WebManageRoles>(keyValues);
        }

        #endregion
    }
}
